//
// Created by zhouwd on 2020/2/25.
//

#include <cstring>
#include <cstdlib>

#include "push_flow.h"

RTMP *rtmp = NULL;

uint32_t startTime;

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_recordingscreen_ZRecordingScreenActivity_pushFlowInit (JNIEnv *env, jclass type, jstring url) {
    ALOGE("Java_com_app_recordingscreen_ZRecordingScreenActivity_pushFlowInit");

    rtmp = RTMP_Alloc();

    if (!rtmp) {
        return -1;
    }
    //初始化
    RTMP_Init(rtmp);

    const char *c_url = env->GetStringUTFChars(url, 0);
    ALOGE("setupURLResult --> c_url --> %s",c_url);
    char *_url = new char(strlen(c_url)+1);
    strcpy(_url, c_url);
    ALOGE("setupURLResult --> _url --> %s",_url);
    rtmp->Link.timeout = 5;
    int setupURLResult = RTMP_SetupURL(rtmp, _url);
    ALOGE("setupURLResult --> %d", setupURLResult);
    if (!setupURLResult) {
        ALOGE("setupURLResult --> SetupURL Err");
        RTMP_Free(rtmp);
        return -2;
    }

    //发布流
    RTMP_EnableWrite(rtmp);

    int rtmpConncet = RTMP_Connect(rtmp, NULL);
    ALOGE("RTMP_Connect --> rtmpConncet --> %d", rtmpConncet);
    if (!rtmpConncet) {
        ALOGE("RTMP_Connect --> rtmpConncet Err");
        RTMP_Close(rtmp);
        RTMP_Free(rtmp);
        return -3;
    }

    int rtmpConnectStream = RTMP_ConnectStream(rtmp, 0);
    ALOGE("RTMP_ConnectStream --> rtmpConnectStream --> %d", rtmpConnectStream);
    if (!rtmpConnectStream) {
        ALOGE("RTMP_ConnectStream --> rtmpConnectStream Err");
        RTMP_Close(rtmp);
        RTMP_Free(rtmp);
        return -4;
    }

    startTime = RTMP_GetTime();



    delete _url;

    return 0;

}

void pushSPSPPS(const char *sps, int spsLen, const char *pps, int ppsLen) {

    int bodySize = spsLen + ppsLen + 16;

    RTMPPacket *rtmpPacket = new RTMPPacket();
    RTMPPacket_Alloc(rtmpPacket, bodySize);
    RTMPPacket_Reset(rtmpPacket);

    char *body = rtmpPacket->m_body;

    int i = 0;
    //frame type(4bit)和CodecId(4bit)合成一个字节(byte)
    //frame type 关键帧1  非关键帧2
    //CodecId  7表示avc
    body[i++] = 0x17;

    //fixed 4byte
    body[i++] = 0x00;
    body[i++] = 0x00;
    body[i++] = 0x00;
    body[i++] = 0x00;

    //configurationVersion： 版本 1byte
    body[i++] = 0x01;

    //AVCProfileIndication：Profile 1byte  sps[1]
    body[i++] = sps[1];

    //compatibility：  兼容性 1byte  sps[2]
    body[i++] = sps[2];

    //AVCLevelIndication： ProfileLevel 1byte  sps[3]
    body[i++] = sps[3];

    //lengthSizeMinusOne： 包长数据所使用的字节数  1byte
    body[i++] = 0xff;

    //sps个数 1byte
    body[i++] = 0xe1;
    //sps长度 2byte
    body[i++] = (spsLen >> 8) & 0xff;
    body[i++] = spsLen & 0xff;

    //sps data 内容
    memcpy(&body[i], sps, spsLen);
    i += spsLen;
    //pps个数 1byte
    body[i++] = 0x01;
    //pps长度 2byte
    body[i++] = (ppsLen >> 8) & 0xff;
    body[i++] = ppsLen & 0xff;
    //pps data 内容
    memcpy(&body[i], pps, ppsLen);


    rtmpPacket->m_packetType = RTMP_PACKET_TYPE_VIDEO;
    rtmpPacket->m_nBodySize = bodySize;
    rtmpPacket->m_nTimeStamp = 0;
    rtmpPacket->m_hasAbsTimestamp = 0;
    rtmpPacket->m_nChannel = 0x04;//音频或者视频
    rtmpPacket->m_headerType = RTMP_PACKET_SIZE_LARGE;
    rtmpPacket->m_nInfoField2 = rtmp->m_stream_id;

    int result = RTMP_SendPacket(rtmp, rtmpPacket, 0);

    ALOGE("pushSPSPPS --> result --> %d", result);

    RTMPPacket_Free(rtmpPacket);
}

void pushDataStream(const char *data, int dataLen, bool keyFrame) {
    RTMPPacket *rtmpPacket = new RTMPPacket();

    int bodySize = dataLen + 9;

    RTMPPacket_Alloc(rtmpPacket, bodySize);
    RTMPPacket_Reset(rtmpPacket);

    char* body = rtmpPacket->m_body;

    int i = 0;

    if (keyFrame) {
        body[i++] = 0x17;
    } else {
        body[i++] = 0x27;
    }

    //fixed 4byte   0x01表示NALU单元
    body[i++] = 0x01;
    body[i++] = 0x00;
    body[i++] = 0x00;
    body[i++] = 0x00;

    //dataLen  4byte
    body[i++] = (dataLen >> 24) & 0xff;
    body[i++] = (dataLen >> 16) & 0xff;
    body[i++] = (dataLen >> 8) & 0xff;
    body[i++] = dataLen & 0xff;

    memcpy(&body[i], data, dataLen);

    rtmpPacket->m_packetType = RTMP_PACKET_TYPE_VIDEO;
    rtmpPacket->m_nBodySize = bodySize;
    uint32_t time = RTMP_GetTime() - startTime;
    ALOGE("pushDataStream --> time --> %d", time);
    rtmpPacket->m_nTimeStamp = time;
    rtmpPacket->m_hasAbsTimestamp = 0;
    rtmpPacket->m_nChannel = 0x04;//音频或者视频
    rtmpPacket->m_headerType = RTMP_PACKET_SIZE_LARGE;
    rtmpPacket->m_nInfoField2 = rtmp->m_stream_id;



    int result = RTMP_SendPacket(rtmp, rtmpPacket, 0);
    ALOGE("pushDataStream --> result --> %d", result);
    RTMPPacket_Free(rtmpPacket);
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_recordingscreen_ZRecordingScreenActivity_pushSPSPPS (JNIEnv *env, jclass type, jbyteArray spsData, jint spsSize, jbyteArray ppsData, jint ppsSize){
    ALOGE("Java_com_app_recordingscreen_ZRecordingScreenActivity_pushSPSPPS");
    jbyte *jSpsData = env->GetByteArrayElements(spsData, FALSE);

    char *_spsData = (char *)(malloc(spsSize));

    memset(_spsData, 0, spsSize);

    memcpy(_spsData, jSpsData, spsSize);

    jbyte *jPpsData = env->GetByteArrayElements(ppsData, FALSE);

    char *_ppsData = (char *)(malloc(ppsSize));

    memset(_ppsData, 0, ppsSize);

    memcpy(_ppsData, jPpsData, ppsSize);

    pushSPSPPS(_spsData, spsSize, _ppsData, ppsSize);

    free(_spsData);
    free(_ppsData);
    return 0;
}

extern "C"
JNIEXPORT jint JNICALL
Java_com_app_recordingscreen_ZRecordingScreenActivity_pushFlowData (JNIEnv *env, jclass type, jbyteArray data, jint size, jboolean iskeyframe){
    ALOGE("Java_com_app_recordingscreen_ZRecordingScreenActivity_pushFlowData");
    jbyte *jdata = env->GetByteArrayElements(data, FALSE);

    char *_data = (char *)(malloc(size));

    memset(_data, 0, size);

    memcpy(_data, jdata, size);

    pushDataStream(_data, size, iskeyframe);

    free(_data);
    return 0;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_app_recordingscreen_ZRecordingScreenActivity_pushFlowDestory (JNIEnv *env, jclass type){



    return 0;
}

